/**
 * HMI 批量删除 Store 模块
 *
 * 使用 Promise 风格包装 API 调用，提供统一的数据管理接口
 * 遵循 Vue3 最佳实践，使用 Pinia 进行状态管理
 */

import { defineStore } from "pinia";
import { HmiBatchDeleteAPI } from "@/api/hmi/hmi-batch-delete.api";
import type {
  HmiBatchDeleteQueryParams,
  HmiBatchDeleteRow,
  HmiBatchDeleteRequest,
  HmiBatchResetRequest,
  HmiBatchTransRequest,
  HmiBatchDeleteResponse,
  HmiBatchPointResponse,
  HmiBatchPointRequest,
  HmiBatchResetResponse,
  HmiBatchTransResponse,
} from "@/api/hmi/hmi-batch-delete.api";

/**
 * HMI 批量删除 Store
 */
export const useHmiBatchDeleteStore = defineStore("hmiBatchDelete", () => {
  /**
   * 获取数据列表
   * @param baseUrl 基础 URL
   * @param queryParams 查询参数
   * @returns Promise<HmiBatchDeleteRow[]>
   */
  function fetchList(
    baseUrl: string,
    queryParams: HmiBatchDeleteQueryParams
  ): Promise<HmiBatchDeleteRow[]> {
    return new Promise<HmiBatchDeleteRow[]>((resolve, reject) => {
      HmiBatchDeleteAPI.getList(baseUrl, queryParams)
        .then((response) => {
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  /**
   * 批量删除数据
   * @param baseUrl 基础 URL
   * @param data 删除请求数据
   * @returns Promise<HmiBatchDeleteResponse[]>
   */
  function batchDelete(
    baseUrl: string,
    data: HmiBatchDeleteRequest
  ): Promise<HmiBatchDeleteResponse[]> {
    return new Promise<HmiBatchDeleteResponse[]>((resolve, reject) => {
      HmiBatchDeleteAPI.batchDelete(baseUrl, data)
        .then((response) => {
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  /**
   * 批量重置数据
   * @param baseUrl 基础 URL
   * @param data 重置请求数据
   * @returns Promise<HmiBatchDeleteResponse[]>
   */
  function batchReset(baseUrl: string, data: HmiBatchResetRequest): Promise<HmiBatchResetResponse> {
    return new Promise<HmiBatchResetResponse>((resolve, reject) => {
      HmiBatchDeleteAPI.batchReset(baseUrl, data)
        .then((response) => {
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  /**
   * 批量传动数据
   * @param baseUrl 基础 URL
   * @param data 传动请求数据
   * @returns Promise<HmiBatchDeleteResponse[]>
   */
  function batchTrans(
    baseUrl: string,
    data: HmiBatchTransRequest[]
  ): Promise<HmiBatchTransResponse> {
    return new Promise<HmiBatchTransResponse>((resolve, reject) => {
      HmiBatchDeleteAPI.batchTrans(baseUrl, data)
        .then((response) => {
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  /**
   * 批量对点行数据
   * @param baseUrl URL
   * @param data 对点数据
   * @returns 对点结果
   */
  function batchPoint(
    baseUrl: string,
    data: HmiBatchPointRequest[]
  ): Promise<HmiBatchPointResponse> {
    return new Promise<HmiBatchPointResponse>((resolve, reject) => {
      HmiBatchDeleteAPI.batchPoint(baseUrl, data)
        .then((response) => {
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  function autoPoint(baseUrl: string): Promise<HmiBatchDeleteResponse[]> {
    return new Promise<HmiBatchDeleteResponse[]>((resolve, reject) => {
      const data = {
        status: 1,
      };
      HmiBatchDeleteAPI.autoPoint(baseUrl, data)
        .then((response) => {
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }
  /**
   * 下载数据
   * @param baseUrl 基础 URL
   * @param id 数据 ID
   * @returns Promise<Blob>
   */
  function downloadData(baseUrl: string, id: number): Promise<Blob> {
    return new Promise<Blob>((resolve, reject) => {
      HmiBatchDeleteAPI.downloadData(baseUrl, id)
        .then((response) => {
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  /**
   * 单行删除数据
   * @param baseUrl 基础 URL
   * @param id 数据 ID
   * @param deleteKey 删除操作的 key
   * @returns Promise<HmiBatchDeleteResponse[]>
   */
  function deleteRow(
    baseUrl: string,
    id: number,
    deleteKey: string = "index"
  ): Promise<HmiBatchDeleteResponse[]> {
    const data: HmiBatchDeleteRequest = {
      [deleteKey]: [id],
    };
    return batchDelete(baseUrl, data);
  }

  /**
   * 单行重置数据
   * @param baseUrl 基础 URL
   * @param id 数据 ID
   * @param deleteKey 重置操作的 key
   * @returns Promise<HmiBatchDeleteResponse[]>
   */
  function resetRow(
    baseUrl: string,
    id: number,
    deleteKey: string = "index"
  ): Promise<HmiBatchDeleteResponse[]> {
    const data: HmiBatchResetRequest = {
      [deleteKey]: [id],
    };
    return batchReset(baseUrl, data);
  }

  /**
   * 单行传动数据
   * @param transApi 传动 API 路径
   * @param rowData 行数据
   * @param updateBodyKey 传动请求体的 key 列表
   * @param updateBodyConfig 传动请求体的固定配置
   * @returns Promise<HmiBatchDeleteResponse[]>
   */
  function transRow(
    transApi: string,
    rowData: HmiBatchDeleteRow,
    updateBodyKey: string[] = [],
    updateBodyConfig: Record<string, any> = {}
  ): Promise<HmiBatchDeleteResponse[]> {
    // 根据配置的 key 提取行数据
    const transBody: Record<string, any> = {};
    updateBodyKey.forEach((key) => {
      if (rowData[key] !== undefined) {
        transBody[key.toLowerCase()] = rowData[key]; // 转换为小写以匹配API要求
      }
    });

    // 合并配置的固定值
    const data: HmiBatchTransRequest = {
      ...transBody,
      ...updateBodyConfig,
    };

    return batchTrans(transApi, [data]);
  }

  return {
    fetchList,
    batchDelete,
    batchReset,
    batchTrans,
    downloadData,
    deleteRow,
    resetRow,
    transRow,
    batchPoint,
    autoPoint,
  };
});

/**
 * Store Hook 函数
 * @returns HmiBatchDeleteStore 实例
 */
export function useHmiBatchDeleteStoreHook() {
  return useHmiBatchDeleteStore();
}
